# -*- coding: utf-8 -*-
# Version: Python 3.9.7
# Author: TRIX
# Date: 2021-09-12 21:27:23
# Use:批量转换word为pdf 保存在word文件夹同级目录
from win32com.client import Dispatch
import os

def wordToPdf(wordFolder):
	'''word文件夹目录 确保里面全是word类型文件'''
	outfolder=wordFolder+'ToPDFed'
	if not os.path.exists(outfolder):
		os.mkdir(outfolder)#如果输出文件夹不存在 创建文件夹
	word = Dispatch('Word.Application')#打开word
	for file in os.listdir(wordFolder):
		doc = word.Documents.Open(wordFolder+'\\'+file)#打开docx
		docDir,docNameE=os.path.split(file)#返回路径与文件名组成的元组 不含/
		docName,extension=os.path.splitext(docNameE)#返回文件名字和后缀(扩展名 含.)组成的元组
		outFile = outfolder+'\\'+docName+'.pdf'
		doc.SaveAs(outFile, FileFormat=17)
		doc.Close()
	word.Quit()

wordToPdf(r'D:\_CodePrograms_\Python_Primary_Programs\document_processing\word_processing\backup')
